package com.core.gui.dialogs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.ItemSelectable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.core.dao.impl.CityDAOImpl;
import com.core.dao.impl.CountryDAOImpl;
import com.core.dao.impl.StateDAOImpl;
import com.core.database.Manager;
import com.core.entities.City;
import com.core.entities.Country;
import com.core.entities.Human;
import com.core.entities.State;
import com.core.gui.components.LocationComboBox;
import com.toedter.calendar.JDateChooser;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class RegistrationDialog extends JDialog {

	Connection connection = Manager.getConnection();
	PreparedStatement ps = null;
	ResultSet rs = null;

	private JPanel contentPanel = new JPanel();
	private JLabel firstNameLabel = new JLabel("Fisrt Name:");
	private JTextField firstNameTextField = new JTextField();
	private JLabel secondNameLabel = new JLabel("Second Name:");
	private JTextField secondNameTextField = new JTextField();
	private JLabel thirdNameLabel = new JLabel("Third Name:");
	private JTextField thirdNameTextField = new JTextField();
	private JLabel lastNameLabel = new JLabel("Last Name:");
	private JTextField lastNameTextField = new JTextField();
	private JLabel nationalIdNumberLabel = new JLabel("National ID Number:");
	private JTextField NationaIdNumberTextField = new JTextField();
	private JLabel birthdateLabel = new JLabel("BirthDate:");
	private JDateChooser birthdateChooser = new JDateChooser();
	private JLabel addressLabel = new JLabel("Address:");
	private JTextArea addressTextArea = new JTextArea();
	private JLabel countryLabel = new JLabel("Country:");
	private LocationComboBox<Country> countryComboBox = new LocationComboBox<Country>();
	private JLabel sateLabel = new JLabel("State:");
	private LocationComboBox<State> stateComboBox = new LocationComboBox<State>();
	private JLabel cityLabel = new JLabel("City:");
	private LocationComboBox<City> cityComboBox = new LocationComboBox<City>();
	private JLabel landPhoneLabel = new JLabel("Land Phone Number:");
	private JTextField landPhoneTextField = new JTextField();
	private JLabel mobilePhoneNumberLabel = new JLabel("Mobile Phone Number:");
	private JTextField mobilePhoneTextField = new JTextField();
	private JLabel emailLabel = new JLabel("E-Mail:");
	private JTextField emailTextField = new JTextField();
	private JLabel departmentLabel = new JLabel("Department Name:");
	private JComboBox departmentComboBox = new JComboBox();
	private JLabel jobTitelLabel = new JLabel("Job Titel:");
	private JComboBox jobComboBox = new JComboBox();
	private JLabel salaryLabel = new JLabel("Salary:");
	private JTextField salaryTextField = new JTextField();
	private JLabel qualificationLabel = new JLabel("Qualification :");
	private JComboBox qualificationComboBox = new JComboBox();
	private JLabel userRoleLabel = new JLabel("User Role:");
	private JComboBox userRoleComboBox = new JComboBox();
	private JLabel usernameLabel = new JLabel("Username:");
	private JTextField usernameTextField = new JTextField();
	private JLabel passwordLabel = new JLabel("Password:");
	private JPasswordField passwordField = new JPasswordField();
	private JLabel repasswordLabel = new JLabel("Re-Password:");
	private JPasswordField repasswordField = new JPasswordField();
	private JSeparator separator = new JSeparator();
	private final JLabel lblNewLabel = new JLabel("New label");

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {

			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (UnsupportedLookAndFeelException e) {
			// handle exception
		} catch (ClassNotFoundException e) {
			// handle exception
		} catch (InstantiationException e) {
			// handle exception
		} catch (IllegalAccessException e) {
			// handle exception
		}
		try {
			RegistrationDialog dialog = new RegistrationDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public RegistrationDialog() {
		usernameTextField.setBounds(66, 318, 165, 20);
		usernameTextField.setColumns(10);
		salaryTextField.setBounds(296, 221, 91, 20);
		salaryTextField.setColumns(10);
		emailTextField.setBounds(44, 159, 322, 20);
		emailTextField.setColumns(10);
		mobilePhoneTextField.setBounds(368, 128, 146, 20);
		mobilePhoneTextField.setColumns(10);
		landPhoneTextField.setBounds(112, 128, 137, 20);
		landPhoneTextField.setColumns(10);
		NationaIdNumberTextField.setBounds(109, 37, 140, 20);
		NationaIdNumberTextField.setColumns(10);
		lastNameTextField.setBounds(540, 8, 86, 20);
		lastNameTextField.setColumns(10);
		thirdNameTextField.setBounds(388, 8, 86, 20);
		thirdNameTextField.setColumns(10);
		secondNameTextField.setBounds(233, 8, 86, 20);
		secondNameTextField.setColumns(10);
		firstNameTextField.setBounds(66, 8, 86, 20);
		firstNameTextField.setColumns(10);
		setBounds(100, 100, 653, 489);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		firstNameLabel.setBounds(10, 11, 55, 14);

		contentPanel.add(firstNameLabel);

		contentPanel.add(firstNameTextField);
		secondNameLabel.setBounds(162, 11, 69, 14);

		contentPanel.add(secondNameLabel);

		contentPanel.add(secondNameTextField);
		thirdNameLabel.setBounds(329, 11, 58, 14);

		contentPanel.add(thirdNameLabel);

		contentPanel.add(thirdNameTextField);
		lastNameLabel.setBounds(484, 11, 54, 14);

		contentPanel.add(lastNameLabel);

		contentPanel.add(lastNameTextField);
		nationalIdNumberLabel.setBounds(10, 40, 97, 14);

		contentPanel.add(nationalIdNumberLabel);

		contentPanel.add(NationaIdNumberTextField);
		birthdateLabel.setBounds(259, 40, 49, 14);

		contentPanel.add(birthdateLabel);
		birthdateChooser.setBounds(311, 37, 115, 20);

		contentPanel.add(birthdateChooser);
		addressLabel.setBounds(10, 65, 46, 14);

		contentPanel.add(addressLabel);
		addressTextArea.setBounds(55, 68, 247, 49);

		contentPanel.add(addressTextArea);
		countryLabel.setBounds(311, 69, 46, 14);

		contentPanel.add(countryLabel);

		countryComboBox.setBounds(354, 66, 114, 20);

		contentPanel.add(countryComboBox);
		sateLabel.setBounds(484, 69, 30, 14);

		contentPanel.add(sateLabel);

		stateComboBox.setBounds(515, 66, 97, 20);

		contentPanel.add(stateComboBox);
		cityLabel.setBounds(311, 98, 23, 14);

		contentPanel.add(cityLabel);

		cityComboBox.setBounds(335, 95, 133, 20);

		contentPanel.add(cityComboBox);
		landPhoneLabel.setBounds(10, 130, 100, 14);

		contentPanel.add(landPhoneLabel);

		contentPanel.add(landPhoneTextField);
		mobilePhoneNumberLabel.setBounds(259, 130, 107, 14);

		contentPanel.add(mobilePhoneNumberLabel);

		contentPanel.add(mobilePhoneTextField);
		emailLabel.setBounds(10, 162, 32, 14);

		contentPanel.add(emailLabel);

		contentPanel.add(emailTextField);
		departmentLabel.setBounds(10, 193, 91, 14);

		contentPanel.add(departmentLabel);
		departmentComboBox.setBounds(103, 190, 247, 20);

		contentPanel.add(departmentComboBox);
		jobTitelLabel.setBounds(10, 224, 44, 14);

		contentPanel.add(jobTitelLabel);
		jobComboBox.setBounds(55, 221, 194, 20);

		contentPanel.add(jobComboBox);
		salaryLabel.setBounds(259, 224, 34, 14);

		contentPanel.add(salaryLabel);

		contentPanel.add(salaryTextField);
		qualificationLabel.setBounds(10, 255, 66, 14);

		contentPanel.add(qualificationLabel);
		qualificationComboBox.setBounds(79, 252, 223, 20);

		contentPanel.add(qualificationComboBox);
		userRoleLabel.setBounds(10, 293, 50, 14);

		contentPanel.add(userRoleLabel);
		userRoleComboBox.setBounds(62, 290, 140, 20);

		contentPanel.add(userRoleComboBox);
		usernameLabel.setBounds(10, 321, 52, 14);

		contentPanel.add(usernameLabel);

		contentPanel.add(usernameTextField);
		passwordLabel.setBounds(10, 352, 50, 14);

		contentPanel.add(passwordLabel);
		passwordField.setBounds(60, 349, 171, 20);

		contentPanel.add(passwordField);
		repasswordLabel.setBounds(10, 381, 67, 14);

		contentPanel.add(repasswordLabel);
		repasswordField.setBounds(80, 378, 152, 20);

		contentPanel.add(repasswordField);
		separator.setBounds(1, 412, 632, 8);

		contentPanel.add(separator);
		lblNewLabel.setBounds(438, 237, 158, 70);

		contentPanel.add(lblNewLabel);

		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {

					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		getData();

	}

	private void aa() {
		try {

			countryComboBox.setPlaces(CountryDAOImpl.getInstance().findAll());
		} catch (Exception e) {
			e.printStackTrace();
		}
		countryComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Country country = (Country) countryComboBox.getModel()
						.getSelectedItem();
				countryComboboxAction(country);
			}
		});

		stateComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (stateComboBox.getModel().getSelectedItem() != null) {
					State state = (State) stateComboBox.getModel()
							.getSelectedItem();
					stateComboboxAction(state);
				}
			}
		});
		cityComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (cityComboBox.getModel().getSelectedItem() != null) {
					City city = (City) cityComboBox.getModel()
							.getSelectedItem();
					cityComboBoxAction(city);
				}
			}
		});
	}

	private void countryComboboxAction(Country country) {
		try {
			cityComboBox.clear();
			stateComboBox.clear();
			stateComboBox.setPlaces(StateDAOImpl.getInstance()
					.findByCountry_id(country.getId()));
			stateComboBox.updateUI();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void stateComboboxAction(State state) {
		try {
			cityComboBox.clear();
			cityComboBox.setPlaces(CityDAOImpl.getInstance().findByState_id(
					state.getId()));
			cityComboBox.updateUI();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void cityComboBoxAction(City city) {

	}

	private void getData() {
		Human human = new Human();
		aa();

		human.setFirst_name(firstNameTextField.getText());
		human.setSecond_name(secondNameTextField.getText());
		human.setThird_name(thirdNameTextField.getText());
		human.setLast_name(lastNameTextField.getText());
		human.setNational_id_number(NationaIdNumberTextField.getText());
		human.setBirthdate(birthdateChooser.getDateFormatString());
		human.setAddress(addressTextArea.getText());
		// human.setCity_id(cityComboBox.getSelectedIndex());
		human.setLand_phone(landPhoneTextField.getText());
		human.setMobile_phone(mobilePhoneTextField.getText());
		human.setEmail(emailTextField.getText());
		human.setDepartment_id(departmentComboBox.getSelectedIndex());
		human.setJob_id(jobComboBox.getSelectedIndex());
		human.setSalary(salaryTextField.getText());
		human.setQualification_id(qualificationComboBox.getSelectedIndex());
		// human.setRole(userRoleComboBox.getSelectedItem().toString());
		userRoleComboBox.getSelectedItem();
		human.setUsername(usernameTextField.getText());
		human.setPassword(passwordField.getText());

	}

}
